const matrix = [
  [0, 2, 2, 0],
  [0, 0, 2, 0],
  [2, 4, 2, 0],
  [2, 4, 2, 2],
]

let maxValue = 0;
Array.prototype.print = function() {
  console.log(this.join('\n'))
}

function move(matrix, direction) {
  // 获取边界
  const rows = matrix.length;
  const cols = matrix[0].length;

  // 判断是否合法下标
  function isRange(i,j) {
    return i >= 0 && i < rows && j >= 0 && j < cols;
  }

  // 获取到下一个的位置
  const nexts = {
    up: (i, j) => [i + 1, j],
    down: (i, j) => [i - 1, j],
    left: (i, j) => [i, j + 1],
    right: (i, j) => [i, j - 1],
  }
  
  function nextPos(i, j) {
    const [mi, mj] = nexts[direction](i, j)
    if (isRange(mi, mj)) {
      return [mi, mj]
    }
    return null
  }
  // 获取下一个非0 的位置
  function nextNotZero(i, j) {
    const pos = nextPos(i, j)
    if (!pos)  return null; // 判断是否越界
    const [mi, mj] = pos;
    if (matrix[mi][mj] === 0) {
      return nextNotZero(mi, mj)
    }
    return pos
  }

  // const t = nextNotZero(0, 1)
  // console.log(t)
  
  let records = new Map()
  function calcMoveNum(originX, originY, targetX, targetY) {
    // let num =0;
    // if (originX - targetX === 0) {
    //  num = Math.abs(originY - targetY)
    // } else if (originY - targetY === 0) {
    //   num =  Math.abs(originX - targetX)
    // }
    records.set(`${originX}-${originY}`, {x: targetX, y: targetY})

  }

  // 计算某个坐标的值，同时递归计算下一个坐标的值
  function calc(i,j) {
    const v = matrix[i][j];
    const pos = nextNotZero(i, j)
    if (!pos) { // 下面都是0的，忽略
      return
    }
    const [mi, mj] = pos;
    const nv = matrix[mi][mj];
    if (v === 0) {
      calcMoveNum(mi, mj, i, j)
      matrix[i][j] = nv;
      matrix[mi][mj] = 0;
      calc(i, j)
    } else if (v === nv) {
      calcMoveNum(mi, mj, i, j)
      matrix[i][j] *=2;
      matrix[mi][mj] = 0;
    }
    calc(...nextPos(i, j))
  }
  // calc(0,0)
  if (direction === 'up') {
    for(let i = 0; i < cols; i++ ) {
      calc(0, i)
    }
  } else if (direction === 'down') {
    for(let i = 0; i < cols; i++ ) {
      calc(rows - 1, i)
    }
  }  else if (direction === 'left') {
    for(let i = 0; i < rows; i++ ) {
      calc(i, 0)
    }
  }  else if (direction === 'right') {
    for(let i = 0; i < rows; i++ ) {
      calc(i, cols - 1)
    }
  }
  return records;
}



// matrix.print();
// console.log('---------')
// let records = move(matrix, 'up')
// matrix.print()
// console.log('============')
// console.log(records)

/**
 * 判断是否还有为 0 的格子， 有则填充数字2 / 4
 * */ 
function fill(matrix) {
    // 获取边界
    const rows = matrix.length;
    const cols = matrix[0].length;
    const Zeros = [];
    for(let i = 0; i < rows; i++) {
      for(let j = 0; j < cols; j++) {
        if (matrix[i][j] === 0) {
          Zeros.push([i, j])
        }
      }
    }
    // Zeros.print()
    if (Zeros.length == 0) {
      throw new Error('闯关失败')
    }
    // 随机生成的初始值
    const randomNum = Math.random() > 0.85 ? 2 : 4;
    // 生成位置
    const randomPos = Math.floor(Math.random() * Zeros.length)
    let [cx, cy] = Zeros[randomPos];
    matrix[cx][cy] = randomNum
    return {
      xy: [cx, cy],
      num: randomNum
    }
}

// console.log('---------')
// fill(matrix)
// console.log('---------')
// matrix.print()